<%#
kind: snippet
name: ansible_provisioning_callback
model: ProvisioningTemplate
snippet: true
description: |
  Setups the one time run of the Ansible Tower / AWX callback script on a host.
  Supports only RPM based distributions. It is only used if the host param "ansible_tower_provisioning"
  is set to true. The actual callback script content is stored in the "ansible_tower_callback_script"
  snippet.

  See https://docs.theforeman.org/nightly/Configuring_Ansible/index-foreman-el.html#provisioning-a-callback-for-a-host_ansible for more details
-%>
<% if host_param_true?('ansible_tower_provisioning') -%>
<%
  rhel_compatible = @host.operatingsystem.family == 'Redhat' && @host.operatingsystem.name != 'Fedora'
  os_major = @host.operatingsystem.major.to_i
  has_systemd = (@host.operatingsystem.name == 'Fedora' && os_major >= 20) || (rhel_compatible && os_major >= 7) || (@host.operatingsystem.name == 'Ubuntu' && os_major >= 15) || (@host.operatingsystem.name == 'Debian' && os_major >= 8)
-%>
<% if has_systemd -%>
<%= save_to_file('/etc/systemd/system/ansible-callback.service',
                 snippet('ansible_tower_callback_service')) %>
# Runs during first boot, removes itself
systemctl enable ansible-callback
<% else -%>
# Assume systemd is not available
<%= save_to_file('/root/ansible_provisioning_call.sh', snippet('ansible_tower_callback_script')) %>
(chmod +x /root/ansible_provisioning_call.sh; crontab -u root -l 2>/dev/null; echo "@reboot /root/ansible_provisioning_call.sh" ) | crontab -u root -
<% end -%>
<% end -%>
